package org.example.myleet.p1436;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

public class Solution {
    public String destCity(List<List<String>> paths) {
        HashSet<String> destCandidate = new HashSet<>();
        HashMap<String, Integer> outCount = new HashMap<>();
        for (List<String> path : paths) {
            outCount.put(path.get(0), outCount.getOrDefault(path.get(0), 0) + 1);
            destCandidate.remove(path.get(0));
            if (!outCount.containsKey(path.get(1))) {
                destCandidate.add(path.get(1));
            }
        }
        return destCandidate.iterator().next();
    }
}
